package com.yxmadoc.umlproject.service.impl;

import com.yxmadoc.umlproject.model.Customer;
import com.yxmadoc.umlproject.repository.CustomerRepository;
import com.yxmadoc.umlproject.service.CustomerService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class CustomerServiceImpl implements CustomerService {

	@Autowired
	private CustomerRepository repository;

	@Override
	public Customer save(Customer customer) {
		customer.setCustomerPassword(DigestUtils.md5Hex(customer.getCustomerPassword()));
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
		String dateString = sdf.format(date);
		customer.setCustomerId("CUST" + dateString);
		return repository.save(customer);
	}

	@Override
	public Customer findOne(String  customerId) {
		return repository.findOne(customerId);
	}


}
